<template>
	<div class="Login">
		<!-- logo -->
		<div class="banner">
			<img src="/static/images/logo.png" alt="" class="logo" />
			<text>JinPin</text>
		</div>
		<!-- 账户输入区 -->
		<div class="account">
			<input type="text" placeholder="请输入用户名" v-model="userName" />
			<input type="password" placeholder="请输入密码" v-model="passWord" />
		</div>
		<!-- 登录按钮 -->
		<div class="loginBtn">
			<button @click="login">登录</button>
			<div class="other">
				<text @click="toFindPassword">忘记密码？点击找回</text>
				<text @click="toRegister">注册账号</text>
			</div>
		</div>
		<!-- 其他登录方式 -->
		<div class="otherLogin">
			<div class="wechat">
				<img src="/static/images/wechat.svg" alt="" class="wechatIcon" />
			</div>
			<div class="pay">
				<img src="/static/images/pay.svg" alt="" />
			</div>
		</div>
		<!-- 用户协议 -->
		<div class="userAgreement">
			<checkbox :v-model="isChecked" @click="changeChecked" />
			<text style="opacity: 0.5">是否同意</text>
			<text style="color: #8276fb">「用户协议」</text>
			<text style="opacity: 0.5">和</text>
			<text style="color: #8276fb">「隐私政策」</text>
		</div>
	</div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { http } from "@/pages/utils/Http";
import type { LoginResponse } from "@/pages/utils/request";
const userName = ref('');
const passWord = ref('');
const isChecked = ref(false);


// 切换用户协议的勾选状态
const changeChecked = () => {
	isChecked.value = !isChecked.value;
	console.log(isChecked.value);
}

const toRegister = () => {
	uni.navigateTo({
		url: '/pages/Login/components/Register/Register'
	});
};

const toFindPassword = () => {
	uni.navigateTo({
		url: '/pages/Login/components/Forget/Forget'
	});
};

const login = async () => {
	// 基本校验
	if (!userName.value || !passWord.value) {
		uni.showToast({ title: '请输入用户名和密码', icon: 'none' });
		return;
	}

	if (!isChecked.value) {
		uni.showToast({ title: '请先同意用户协议', icon: 'none' });
		return;
	}

	try {
		uni.showLoading({ title: '正在登录...' });
		const res = await http.post<LoginResponse>('/api/login', {
			username: userName.value,
			password: passWord.value
		});
		uni.hideLoading();

		if (res.code === 200) {
			// 缓存用户信息，供后续页面使用
			uni.setStorageSync('userId', res.data.id);
			uni.setStorageSync('username', res.data.username);
			// TODO: 若后端返回 token，请一并缓存
			
			uni.showToast({ title: res.message || '登录成功', icon: 'success' });
			// 登录成功后 1 秒跳转到首页
			setTimeout(() => {
				uni.reLaunch({ url: '/pages/Layout/Layout' });
			}, 1000);
		} else {
			uni.showToast({ title: res.message || '登录失败', icon: 'none' });
		}
	} catch (err: any) {
		uni.hideLoading();
		uni.showToast({ title: err?.message || '登录失败', icon: 'none' });
	}
};
</script>

<style scoped lang="scss">
.Login {
	width: 100%;
	max-width: 500px;
	height: 100vh;
	/* 统一使用100vh */
	display: flex;
	flex-direction: column;
	align-items: center;
	margin: 0 auto;
	padding: 0;
	position: relative;
	overflow-y: auto;
	/* 添加滚动以防内容超出 */
}

.banner {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	margin-top: 100px;

	text {
		font-size: 25px;
		font-weight: bolder;
	}

	.logo {
		width: 200rpx;
		height: 200rpx;
	}
}

.account {
	margin-top: 50rpx;
	width: 95%;
	display: flex;
	flex-direction: column;
	align-items: center;

	input {
		width: 95%;
		height: 50px;
		border: 1px solid #ccc;
		border-radius: 0rpx 0rpx 15rpx 15rpx;
		padding-left: 10px;
		font-size: 16px;

		/* 宽屏输入框优化 */
		@media (min-width: 500px) {
			width: 400px;
		}
	}

	input:first-child {
		border-radius: 15rpx 15rpx 0rpx 0rpx;
		border-bottom: none;
	}
}

.loginBtn {
	margin-top: 50rpx;
	width: 95%;

	/* 宽屏按钮优化 */
	@media (min-width: 500px) {
		width: 400px;
	}

	button {
		height: 70rpx;
		background-color: #6f76fb;
		border-radius: 10px;
		border: none;
		color: white;
		line-height: 70rpx;
		font-size: 16px;
		cursor: pointer;
		transition: background-color 0.3s;

		&:hover {
			background-color: #5a62d8;
		}
	}
}

.other {
	margin-top: 15rpx;
	display: flex;
	justify-content: space-between;
	font-size: 25rpx;
	width: 100%;

	text {
		cursor: pointer;
		transition: color 0.3s;

		&:hover {
			color: #6f76fb;
		}
	}
}

.otherLogin {
	margin-top: 50rpx;
	display: flex;
	align-items: center;
	width: 80%;
	justify-content: space-evenly;

	/* 宽屏社交登录优化 */
	@media (min-width: 500px) {
		width: 400px;
	}

	.wechat {
		background-color: #41db24;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		cursor: pointer;
		transition: transform 0.3s;

		&:hover {
			transform: scale(1.1);
		}

		.wechatIcon {
			height: 60%;
			width: 60%;
		}
	}

	.pay {
		background-color: #6faafb;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		cursor: pointer;
		transition: transform 0.3s;

		&:hover {
			transform: scale(1.1);
		}

		img {
			height: 80%;
			width: 80%;
		}
	}
}

.userAgreement {
	margin-top: 100rpx;
	font-size: 25rpx;
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	justify-content: center;
	text-align: center;

	text {
		margin: 0 4px;
		cursor: pointer;
	}

	/* 宽屏协议文字优化 */
	@media (min-width: 500px) {
		margin-top: 80rpx;
		width: 400px;
	}
}
</style>
